<template>
	<view class="">
		<view class="school-search-header">
			<view style="margin-bottom: 30rpx;">
				<u-search placeholder="请输入学生名称" v-model="name" :showAction="false" :clearabled="false"></u-search>
			</view>
			<!-- 	<u-search placeholder="请输入学生身份证" v-model="identity_card" :showAction="false" :clearabled="false"></u-search> -->
			<view class="header-search-btnview">
				<view class="header-search-btn" @click="getStudentList">搜索</view>
				<view class="header-search-btn1" @click="handleReset">重置</view>
			</view>
		</view>
		<view class="list">
			<view class="school-item" v-for="(item,index) in list" :key="index">
				<view style="display: flex;align-items: center;">
					<view class="school-title">{{item.name}}</view>
					<view style="color: #999999;font-size: 13px;margin-left: 20rpx;">{{item.identity_card}}</view>
				</view>
				<view class="students-btn" @click="handlebindUser(item)" v-if="item.is_bind == 0">确认绑定</view>
				<view class="students-btn1" v-else>已绑定</view>
			</view>
		</view>
		<view v-if="list.length == 0">
			<u-empty mode="list">
			</u-empty>
		</view>


		<!-- 底部操作栏 -->
		<!-- <view style="height: 200rpx;"></view>
		<view class="school-bottom-view">
			<view class="school-bootom-btn" @click="handleAddUser">
				确认添加
			</view>
		</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: '', // 学生姓名
				identity_card: '', // 身份证	
				list: [],
				student_list: [], // 学生列表
				isChoose: false,
				itemId:''
			}
		},
		onShow() {
			if (uni.getStorageSync('student_list')) {
				this.student_list = uni.getStorageSync('student_list')
			}
		},
		methods: {
			// 绑定学生
			handlebindUser(item){
				this.$api.post(global.apiUrls.bingUser, {
					student_id: item.id
				}).then(res => {
					uni.showToast({
						title:res.data.msg,
						icon:'none'
					})
					item.is_bind = 1
				})
			},
			// 选择学生
			// chooseIsName(item) {
			// 	console.log(item)
			// 	if(this.itemId == item.id){
			// 		this.list.forEach(v=>{
			// 			v.isChoose = false
			// 		})
			// 	}else{
			// 		this.list.forEach(v=>{
			// 			v.isChoose = false
			// 		})
			// 		item.isChoose = true
			// 	}
			// 	this.itemId = item
			// },
			// 添加学生
			// handleAddUser() {
			// 	if (this.list.length == 0) {
			// 		return uni.showToast({
			// 			title: '请添加学生'
			// 		})
			// 	}
			// 	if (this.student_list.length == 0) {
			// 		this.student_list.push(this.list[0])
			// 		console.log(this.student_list)
			// 		uni.setStorageSync('student_list', this.student_list)
			// 	} else {
			// 		let arr = []
			// 		console.log(this.student_list.indexOf(this.list[0]))
			// 		arr = this.student_list.filter(item => {
			// 			return item.id == this.list[0].id
			// 		})
			// 		console.log(arr)
			// 		if (arr.length > 0) {
			// 			this.student_list = this.student_list.filter(item => {
			// 				return item.id !== this.list[0].id
			// 			})
			// 			this.student_list.push(this.list[0])
			// 			uni.setStorageSync('student_list', this.student_list)
			// 		} else {
			// 			this.student_list.push(this.list[0])
			// 			uni.setStorageSync('student_list', this.student_list)
			// 		}
			// 	}
			// 	uni.navigateBack({
			// 		delta: 1
			// 	})
			// },
			// 搜索重置
			handleReset() {
				this.name = ''
				this.identity_card = ''
				this.list = []
			},
			// 学生列表
			getStudentList() {
				if (!this.name) {
					return uni.showToast({
						title: '请填写学生姓名',
						icon: 'none'
					})
				}
				// if(!this.identity_card) {
				// 	return uni.showToast({
				// 		title:'请填写学生身份证',
				// 		icon:'none'
				// 	})
				// }
				this.$api.post(global.apiUrls.studentList, {
					name: this.name,
					identity_card: this.identity_card
				}).then(res => {
					let arr = []
					arr = res.data.data.map(item => {
						return {
							id: item.id,
							name: item.name,
							identity_card: item.identity_card.replace(/(\w{6})\w*(\w{4})/, '$1********$2'),
						    is_bind:item.is_bind,
							isChoose:false
						}
					})
					this.list = arr
					// this.list = res.data.data
					console.log(this.list)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.students-btn1{
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 10rpx 20rpx;
		background-color: #f4f4f4;
		border-radius: 10rpx;
		font-size: 13px;
		color: #999999;
	}
	.students-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 10rpx 20rpx;
		background-color: #FC6A84;
		border-radius: 10rpx;
		font-size: 13px;
		color: #ffffff;
	}
	.header-search-btn1 {
		width: 150rpx;
		height: 60rpx;
		font-size: 15px;
		color: #999999;
		background-color: #E4E7ED;
		border-radius: 10rpx;
		text-align: center;
		line-height: 60rpx;
		margin-right: 20rpx;
	}

	.header-search-btn {
		width: 150rpx;
		height: 60rpx;
		font-size: 15px;
		color: #ffffff;
		background-color: #FC6A84;
		border-radius: 10rpx;
		text-align: center;
		line-height: 60rpx;
	}

	.header-search-btnview {
		display: flex;
		align-items: center;
		flex-direction: row-reverse;
		margin-top: 30rpx;
	}

	.school-bootom-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 15px;
		color: #ffffff;
		padding: 20rpx;
		background-color: #FC6A84;
		border-radius: 50rpx;
	}

	.school-bottom-view {
		width: 100%;
		position: fixed;
		bottom: 0rpx;
		left: 0;
		padding: 30rpx;
		background-color: #ffffff;
	}

	.school-title {
		font-size: 15px;
		color: #000000;
	}

	.school-item:last-child {
		margin-bottom: 0;
	}

	.school-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 30rpx;
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 30rpx;
	}

	.list {
		padding: 30rpx;
	}

	.school-search-header {
		background-color: #ffffff;
		padding: 30rpx;
	}
</style>